/**
 * @author 徐楠
 * @date 2022/4/7 22:54
 * @version 1.0
 */

package com.xunan.likou;

import java.util.ArrayList;
import java.util.List;

public class RemodelingMatrix {
    public static void main(String[] args) {
        int[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[][] ints = matrixReshape(mat, 3, 3);
        for (int[] anInt : ints) {
            for (int i : anInt) {
                System.out.println(i);
            }
        }
    }

    public static int[][] matrixReshape(int[][] mat, int r, int c) {
        //列长
        int x = mat.length;
        //行长
        int y = mat[0].length;

        if (r * c != (mat[0].length * mat.length)) {
            return mat;
        }
        List<Integer> list = new ArrayList();
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                list.add(mat[i][j]);
            }
        }
        int index = 0;
        int[][] result = new int[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                result[i][j] = list.get(index++);
            }
        }
        return result;

    }
}
